package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import dao.AnnounceDAO;
import dao.AnnounceDaoJdbcImpl;
import dao.ClassNewsDAO;
import dao.ClassNewsDaoJdbcImpl;
import dao.HomeworkDAO;
import dao.HomeworkDaoJdbcImpl;
import dao.NewsDAO;
import dao.NewsDaoJdbcImpl;
import dao.UserInfoDAO;
import dao.UserInfoDaoJdbcImpl;
import modle.announce;
import modle.classnews;
import modle.homework;
import modle.news;
import modle.userInfo;

/**
 * Servlet implementation class ArticleServlet
 */
@WebServlet("/ArticleServlet")
public class ArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArticleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("::::::::::::::ArticleServlet:::::::::::");
		
		//获取信息
		JSONObject jsonObject = new JSONObject();
		//获取文章信息
		String id = request.getParameter("id");
		int ID = Integer.parseInt(id); 
		String page = request.getParameter("page");	
		System.out.print(id+","+page);
		if(page.equals("announce")) {	//学校通知
			AnnounceDAO ad = new AnnounceDaoJdbcImpl();
			announce announce = null;
			announce = ad.readAnnounce(ID);
			jsonObject.put("announce",announce);
		}else if( page.equals("homework")) {	//作业
			HomeworkDAO hwd = new HomeworkDaoJdbcImpl();
			homework homework = null;
			homework = hwd.readHomework(ID);
			jsonObject.put("homework", homework);
		}else if( page.equals("classnews")) {	//班级新闻
			ClassNewsDAO cnD = new ClassNewsDaoJdbcImpl();
			classnews classNews = null;
			classNews = cnD.readClassNews(ID);
			jsonObject.put("schoolnews", classNews);
		}
		
		System.out.println("::::::::::::::end::ArticleServlet:::::::::::");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String jsonStr = JSON.toJSONString(jsonObject);
		PrintWriter out = response.getWriter();
		System.out.println(jsonStr);
		out.println(jsonStr);
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
